<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Kra8\Snowflake\HasSnowflakePrimary;

class Group extends Model
{
    //
    use SoftDeletes;
    use HasSnowflakePrimary;
    protected $fillable = [
        'cover',
        'name',
        'policy_id',
        'cate',
        'dividend_cate',
        'dividend_scale',
        'device_group_id',
        'people_total',
        'user_id',
    ];
    
    public $incrementing = false;
    
    public function author()
    {
        return $this->belongsTo('App\Models\User', 'user_id');
    }
    public function policy()
    {
        return $this->belongsTo('App\Models\GroupPolicy', 'policy_id');
    }
    public function members()
    {
        return $this->hasMany('App\Models\GroupMember', 'group_id');
    }
    public function devices()
    {
        return $this->hasMany('App\Models\Device', 'device_group_id');
    }
    public function inspection_logs()
    {
        return $this->hasMany('App\Models\GroupInspectionLog', 'device_group_id');
    }
    public function getMerchantAttribute(){
        return $this->members()->where('role','MERCHANT')->first();   
    }
}
